<?php  
	include_once(dirname(__FILE__) . "/model/nms_alarm_model.php");
	include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
	include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
	include_once(dirname(__FILE__) . "/model/nms_alarm_model.php");
  	function draw_log_interface(){
  		html_master_script_filter();
  		html_log_interface_script();
  		draw_log_interface_header();
  		//xac dinh bao nhiu dong
  		$display =50;
  		//tinh so trang can hien thi
  		//begin get query string from url to decide what relevant data
  		$sql_where  ="";
  		/* if the user pushed the 'clear' button */
  		if (isset($_REQUEST["clear_x"])) {
  			unset($_REQUEST["host_id"]);
  			unset($_REQUEST["search"]);
  			unset($_REQUEST["filter"]);
  			$_REQUEST["id"]=0;
  			$_REQUEST['run']='alarm';
  		
  		}
  		/* get value from action filter host*/
  		if (!empty($_GET['host_id'])) {
  			if($_GET['host_id']=='null')
  			{
  				$sql_where.="";
  			}else{
  				$sql_where .="and b.host_id =".$_GET['host_id']."";
  			}
  		}
  		/* get value from action filter status*/
  		if(isset($_REQUEST['filter'])){
  			if($_REQUEST['filter']=="All"){
  				$sql_where .="";
  			}
  			if($_REQUEST['filter']=="Up"){
  				$sql_where.=" and a.nms_status='Up'";
  			}
  			if($_REQUEST['filter']=="Down"){
  				$sql_where.=" and a.nms_status='Down'";
  			}
  			if($_REQUEST['filter']=="Pause"){
  				$sql_where.=" and b.nms_pause='0'";
  			}
  			if($_REQUEST['filter']=="Unknown"){
  				$sql_where.=" and a.nms_status='Unknown'";
  			}
  		}
  		/* get value from action search information*/
  		if (!empty($_REQUEST['search'])) {
  			$research=trim($_REQUEST['search']);
  			$sql_where.=" and (b.snmp_index like '%".$research."%' or c.description like '%".$research."%' or c.hostname like '%".$research."%' or CONCAT(c.description,' ','(',c.hostname,')') like '%".$research."%'
                                                or CONCAT('(',b.interface_name,')','\t',b.interface_alias) like '%".$research."%' or b.interface_name like '%".$research."%' or b.interface_alias like '%".$research."%')";
  		}
  		/* Get total interface_id from table plugin_nms_interface*/
  		
  		
  		
  		if(empty($_GET['record']))
  		{
  			$getrecord=50;
  		}else{
  			$getrecord=$_GET['record'];
  		}
  		$query="SELECT count(a.interface_id) FROM (SELECT a.interface_id FROM plugin_nms_log a ,plugin_nms_interface b ,host c
                                where a.interface_id=b.interface_id and b.present =1 and b.host_id =c.id ".$sql_where."
  		                                limit 0,$getrecord) as a";
  		$record =db_fetch_cell($query);
  		if ($record > $display){
  			$page = ceil($record/$display);
  		}else {
  			$page = 1;
  		}
  		if(!empty($_REQUEST['id'])){
  			$id=$_REQUEST['id'];
  		}else{
  			$id=0;
  		}
  		$start = $id;
  		$current = ($start/$display)+1;
  		$next = $start + $display;
  		$previous = $start - $display;
  		$last = ($page - 1)*$display;
  		if ($current >= 11) {
  			$start_page = $current - 5;
  			if ($page > $current + 5)
  				$end_page = $current + 5;
  			else if ($current <= $page && $current > $page - 10) {
  				$start_page = $page - 10;
  				$end_page = $page;
  			} else {
  				$end_page = $page;
  			}
  		} else {
  			$start_page = 1;
  			if ($page > 11)
  				$end_page = 11;
  			else
  				$end_page = $page;
  		}
  		$querydata="SELECT a.interface_id as id,b.snmp_index as snmp_index ,b.host_id as host_id, b.interface_name as descrip , b.interface_alias as alias , a.nms_status as nms_status , a.nms_time as nms_time ,CONCAT_WS('',c.description,' (',c.hostname,')') as host_name,a.user_active as user
                                        ,CONCAT('(',b.interface_name,')','\t',b.interface_alias) as interface_des,a.send_sms
                                        FROM plugin_nms_log a ,plugin_nms_interface b, host c
                                        where a.interface_id=b.interface_id and b.present =1 and b.host_id = c.id ".$sql_where."
  		                                        order by a.nms_time DESC limit $start,$display";
  		draw_log_interface_nav($page,$previous,$display,$next,$last,$current,$start_page,$end_page);
  		draw_log_interface_table($querydata);
  		if (isset($_GET['search'])) {
  			?>
  		   <script >
  		       var tmp = $('#id_master_table tr').removeHighlight().highlight('<?php echo trim($_GET['search']) ?>');
  		       alert(test);
  		       $('#id_master_table').empty();
  		       $('#id_master_table').html(test);
  		   </script>
  		    <?php 
  		         }
  		draw_log_interface_nav($page,$previous,$display,$next,$last,$current,$start_page,$end_page); 
  		html_master_footer();
  	}
  	function draw_log_interface_header(){
  		html_master_header_start("Host Load Filter");
	?>
                <table width="100%" border="0">
                        <tr>
                        <!-- Status filter -->
                                <td width="3%">Status:</td>
                                <td width="5%">
                                        <select onchange="val()"  id="id_status_alarm1">
                                                <option value="All">All</option>
                                            <option value="Up">Up</option>
                                            <option value="Down">Down</option>
                                            <option value="Unknown">Unknown</option>
                                        </select>
                                </td>
                                <!--  -->
                                <!-- Devices filter -->
                                <td width="3%" >Device:</td>
                                <td>
                                        <select  onchange="val()" id="id_host_alarm1">
                                                <option value="">Any</option>
                                                <?php
                                                 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host where description !='Localhost' order by description,hostname");      $result3 = db_fetch_assoc($sql54);
                                                 if (sizeof($hosts) > 0) {
                                                     foreach($hosts as $set) {
                                                     echo "<option value='" . $set["id"] . "'";
                                                     if(isset($_REQUEST["host_id"])){if($_REQUEST["host_id"] == $set["id"]){echo "selected";}};
                                                      echo ">" . htmlspecialchars($set["name"]) . "</option>\n";
                                                     }
                                                     }
                                                        ?>
                                        </select>
                                       <script type="text/javascript">
											$("#id_host_alarm1").chosen({
												search_contains: true,
												no_results_text:"Oops, nothing found!"});
											$("#id_host_alarm1").on('change',function(){
											 	//get current selected value of combobox
												var statusValue = $(this).val();
											 	var deviceValue = $("#id_host_alarm1").val();
											 	 var searchFilter = $("#id_search1").val();
											 	var record= $("#id_status_alarm2").val();
											 	//build query string
											 	var queryString = "run=log&tab=log_interface&id=0&filter="+statusValue+"&host_id="+deviceValue+"&record="+record+"&search="+searchFilter;
											 	document.location.href = "?"+queryString;
											});
 					 				 </script>
                                </td>
                                <!--  -->
                                <!-- Button export -->
                                        <!-- Search -->
                                        <td>Search</td>
                                        <td colspan="3" scope="col">
                                        <input id="id_search1" type="search" onkeypress="keydown(event)"/>
                                </td>
                                <td>&nbsp;</td>
                                <td>
                                        <input id="btnGo1" type="button" value="Go" ;">
                                </td>
                                <td>&nbsp;</td>
                                <td>
                                        <input id="btnClear1" type="submit" name="clear_x" value="Clear" title="Clear Filters">
                                </td>
                                <td>&nbsp;</td>
                                <td>
                                        <input id="btnExport1" type="button" value="Export" onclick="location.href='';">
                                </td>
                                <td>&nbsp;</td>
                                        <td>
                                                <input id="btnRefresh1" type="button" value="Refresh" onclick="location.href='';">
                                        </td>
                                        <td width="3%">Record:</td>
                                <td width="5%">
                                        <select onchange="val()"  id="id_status_alarm2">
                                                <option value="50">Default</option>
                                                <option value="100">100</option>
                                            <option value="200">200</option>
                                            <option value="500">500</option>
                                            <option value="1000">1000</option>
                                            <option value="2000">2000</option>
                                            <option value="3000">3000</option>
                                        </select>
                                </td>
                            </tr>
                </table>                
<?php 
  		html_master_header_end();
  	}
  	function draw_log_interface_nav($page,$previous,$display,$next,$last,$current,$start_page,$end_page){
  		html_master_nav_page_start();
  		 if (empty($_GET['filter'])) {
                $status="All";
        }
        else{
                $status =  $_GET['filter'];
        }
        if (empty($_GET['host_id'])) {
                $host_id="";
        }
        else{
                $host_id =  $_GET['host_id'];
        }
        if (empty($_GET['record'])) {
                $record1="50";
        }
        else{
                $record1 =  $_GET['record'];
        }
        if (empty($_GET['search'])) {
                $search="";
        }
        else{
                $search =  $_GET['search'];
        }

    ?>
    <script type="text/javascript">
        $("#id_status_alarm1").val('<?php echo $status;?>');
        $("#id_host_alarm1").val('<?php echo $host_id;?>');
        $("#id_status_alarm2").val('<?php echo $record1;?>');
        $("#id_search1").val('<?php echo $search;?>');
    </script>
    <?php 
                if (isset($_REQUEST["btnClear"])) {
                        unset($_REQUEST["#id_search"]);
                        unset($_REQUEST["id_host_alarm"]);
                        unset($_REQUEST["id_status_alarm"]);
                }
                //build where status query
                if(isset($_GET['filter']) or isset($_GET['host_id']) or isset($_GET['search']) or isset($_GET['record'])){
                        $base_url = "&filter=".$status."&host_id=".$host_id."&record=".$record1."&search=".$search."";
                }
                 else{
                $base_url=null;
                }
                        if($page > 1) {
                if ($current > 1) {
                    echo "<li><a href='nms.php?run=log&tab=log_interface&id=0$base_url'>First</a></li>";
                    echo "<li><a href='nms.php?run=log&tab=log_interface&id=$previous$base_url'>Previous</a></li>";
                } 
               for ($i = $start_page; $i <= $end_page; $i++) {
         
                    if ($current == $i)
                        echo "<li class='current'>$i</li>";
                    else
                        echo "<li><a href='nms.php?run=log&tab=log_interface&id=".($display*($i-1))."$base_url'>$i</a></li>";
                }
                if ($current < $page) {
                    echo "<li><a href='nms.php?run=log&tab=log_interface&id=$next$base_url'>Next</a></li>";
                    echo "<li><a href='nms.php?run=log&tab=log_interface&id=$last$base_url'>Last</a></li>";
                   } 
                }            
  	 		html_master_nav_page_end();
  	 	}
  	 	function draw_log_interface_table($querydata){
  	 		html_master_table_start("100%", "center");
  	 		?>
   							  <th width="3%" scope="col">No</th>
                            <th width="5%" scope="col">Index</th>
                            <th width="15%" scope="col">Host name</th>
                            <th width="30%" scope="col">Interface</th>
                            <th width="6%" scope="col">Status</th>
                            <th width="12%" scope="col">Time</th>
                            <th width="6%" scope="col">User</th>
                            <th width="6%" scope="col">SMS</th>
                        </tr>
                        
                                <!-- play sound when devices down -->
                                        <?php
                                        ?>              
                                        <!-- do du lieu vao bang -->
                                        <?php 
                                        $result = db_fetch_assoc($querydata);
                                        foreach($result as $set) {
                                        //$id1 = $set['id'];
                                        ?>
                                        <!-- thay doi mau cho cac trang thai trong bang -->
                                        <tr  <?php
                                         if($set['nms_status']=='Down') {echo 'data-status ="1" class="mautrangthaiDown"';}
                                         elseif($set['nms_status']=='Up'){echo 'data-status ="2" class="mautrangthaiUp"';}
                                         elseif($set['nms_status']=='Unknown'){echo 'data-status ="3" class="mautrangthaiUnk"';}?>>
                                                <td align="center" id="getpost">&nbsp;<?php echo $set['id']?></td>
                                            <td align="center">&nbsp;<?php echo $set['snmp_index']?></td>
                                            <td >&nbsp;<?php echo $set['host_name']?></td>
                                            <td >&nbsp;<?php echo $set['interface_des']?></td> 
                                            <td align="center">&nbsp;<?php echo $set['nms_status']?></td> 
                                            <td align="center">&nbsp;<?php  echo $set['nms_time'] ?></td>                                   
                                        <td align="center">&nbsp;<?php echo  $set['user']?></td>        
                                        <?php
                                        if($set['send_sms']!=''){
                                                $arry = explode(' ',$set['send_sms']);
                                                //echo count($arry);
                                                 if(count($arry)<2){
                                                                echo '<td align="center">'.$set['send_sms'].'</td>';
                                                         }
                                                         else{
                                                                echo '<td align="center"><a href="#" TITLE="';
                                                                        for($i=1;$i<count($arry);$i++){
                                                                        echo $arry[$i].'&#10 ';
                                                                        }
                                                                        echo '">Fail</a></td>';
                                                         }
                                                }
                                                else {
                                                        echo '<td align="center">&nbsp;'.$set['send_sms'].'</td>';
                                                }
                                        ?>      
                                        </tr>
                                <?php              
                                }
                                ?>
  	 				</table>
 <?php 
  	 	 	}
  function html_master_footer(){
  	?>
  	 		<div id="center_footer_up">
				  	 	 			<div class="countdown_time"  >
				  	 	 				<h3><div id="Timerefresh">Refresh time: </div></h3>
				  	 	 				<h3><div id ="countdown_time2"></div></h3>
				  	 	 			</div>
			</div>
  	 	 	<?php 	
  	html_alarm_script_time();
  }
?>
